<template>
	<view class="slider-verify-box" v-if="isShow">
		<view class="verifyBox">
			<!-- <view class="slider-title">图形验证</view> -->
			<view class="slide-content">
				
				<view class="slider-pintu">
					<image id="pintuImg" :src="'https://img.btcys.com/codeMessage/' + img + '.png'" class="pintu"></image>
					<view class="pintukuai" :style="{ top: top + 'px', left: oldx + 'px' }">
						<!-- <image :src="'https://imgcdn.ecbao.cn/nft/slider-verify/' + img + '.jpg'" :style="{ top: '-' + top + 'px', left: '-' + left + 'px'}"></image> -->
						<image :src="'https://img.btcys.com/codeMessage/' + img + '.png'" :style="{ top: '-' + top + 'px', left: '-' + left + 'px'}"></image>
					</view>

					<view class="yinying" :style="{ top: top + 'px', left: left + 'px' }"></view>
				</view>

				<view class="slider-movearea" @touchend="endTouchMove">
					<movable-area :animation="true">
						<movable-view :x="x" direction="horizontal" @change="startMove">
							<view></view>
							<view></view>
							<view></view>
						</movable-view>
					</movable-area>

					<view class="huadao">拖动左边滑块完成上方拼图</view>
				</view>
			</view>
			
			<view class="slider-btn-group">
				<view class="slider-btn" @tap="closeSlider">关闭</view>
				<view class="slider-btn slide-btn-refresh" @tap="refreshVerify">刷新</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	name: 'slider-verify',
	props: {
		isShow: true
	},
	data() {
		return {
			x: 0, //初始距离
			oldx: 0, //移动的距离
			img: '1', //显示哪张图片
			left: 0, //随机拼图的最终X轴距离
			top: 0, //拼图的top距离
		};
	},
	watch: {
		// 每次打开重新刷新拼图
		isShow(newValue, oldValue) {
			if(newValue){
				this.refreshVerify();	//刷新
			}
		}
	},
	mounted() {
		var that = this;
		that.refreshVerify();
	},
	methods: {
		//刷新验证
		refreshVerify() {
			var gl = Math.random().toFixed(2);
			this.left = uni.upx2px(560) * gl > uni.upx2px(280) ? uni.upx2px(280) : uni.upx2px(560) * gl + uni.upx2px(150); //生成随机X轴最终距离
			this.top = uni.upx2px(190) * gl; //生成随机Y轴初始距离
			
			if (gl <= 0.25) {
				this.img = 1;
			}
			if (gl > 0.25 && gl <= 0.5) {
				this.img = 2;
			}
			if (gl > 0.5 && gl <= 0.75) {
				this.img = 3;
			}
			if (gl > 0.75 && gl <= 1) {
				this.img = 4;
			}
			
			this.resetMove();	//重置阴影位置
		},

		/* 滑动中 */
		startMove(e) {
			this.oldx = e.detail.x;
		},

		/* 滑动结束 */
		endTouchMove() {
			var that = this;

			if (Math.abs(that.oldx - that.left) <= 5) {
				// uni.showToast({
				// 	title: '验证成功',
				// 	duration: 2500,
				// 	success() {
						
				// 	}
				// });
				that.$emit('touchSliderResult', true);
			} else {
				uni.showToast({
					title: '验证失败',
					icon: "none",
					duration: 2500,
					success() {
						that.refreshVerify();
					}
				});
				
			}
		},
		
		/* 重置阴影位置 */
		resetMove() {
			this.x = 1;
			this.oldx = 1;
			setTimeout(() => {
				this.x = 0;
				this.oldx = 0;
			}, 300);
		},
		
		// 关闭
		closeSlider(){
			this.$emit('touchSliderResult', false);
		}	
	}
};
</script>

<style scoped>
.slider-verify-box {
	position: fixed;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	background-color: rgba(0, 0, 0, 0.5);
	z-index: 999;
}
.verifyBox {
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
	background-color: #fff;
	border-radius: 20upx;
}
.verifyBox .slider-title {
	font-size: 36upx;
	text-align: center;
	padding: 1em 0;
	color: rgba(2, 20, 33, 0.85);
}

.verifyBox .slide-content {
	width: 560upx;
	padding: 1em 1em 0;
	margin: 0 auto;
}

.verifyBox .slide-content .slider-pintu {
	position: relative;
	width: 100%;
	border-radius: 10upx;
	overflow: hidden;
}
.verifyBox .slide-content .pintu {
	width: 560upx;
	height: 315upx;
	display: block;
	margin: 0 auto;
}
.verifyBox .slide-content .pintukuai {
	position: absolute;
	top: 0;
	left: 0;
	width: 100upx;
	height: 100upx;
	z-index: 100;
	box-shadow: 2upx 0 16upx rgba(0,0,0,.9);
	overflow: hidden;
}
.verifyBox .slide-content .pintukuai image {
	display: block;
	position: absolute;
	top: 0;
	left: 0;
	width: 560upx;
	height: 315upx;
}
.verifyBox .slide-content .yinying {
	position: absolute;
	width: 100upx;
	height: 100upx;
	background-color:#fff;
	box-shadow: 2upx 0 16upx rgba(0,0,0,.9) inset;
}	
.slider-movearea {
	position: relative;
	height: 66upx;
	width: 100%;
	margin: 25upx auto;
}
.slider-movearea movable-area {
	height: 66upx;
	width: 100%;
}
.slider-movearea movable-view {
	position: relative;
	display: flex;
	align-items: center;
	justify-content: center;
	width: 100upx;
	height: 100%;
	background-color: #fff;
	border-radius: 6upx;
	box-shadow: 0 0 8upx #ccc;
	cursor: pointer;
	z-index: 100;
}
.slider-movearea movable-view>view {
    width: 4upx;
    height: 40%;
	border-radius: 2upx;
    transition: all .2s;
    background:#6aa0ff;
}
.slider-movearea movable-view>view:nth-child(3) {
    margin: 0 4px;
}
.huadao {
	width: 100%;
	height: 66upx;
	line-height: 66upx;
	background: #eef1f8;
	box-sizing: border-box;
	position: absolute;
	top:0;
	left: 0;
	z-index: 99;
	border-radius: 6upx;
	box-shadow: inset 0 0 16upx hsl(0deg 0% 94% / 60%);
	font-size: 28upx;
	color: #b7bcd1;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
	text-align: center;
	width: 100%;
}
.slider-btn-group{
	width: 100%;
	display: flex;
	justify-content: center;
	align-items: center;
	border-top: 1px solid #ddd;
}
.slider-btn-group .slider-btn{
	flex: 1;
	height: 100upx;
	line-height: 100upx;
	text-align: center;
	font-size: 32upx;
	color:rgba(2,20,33,0.85);
	
}
.slider-btn-group .slider-btn:active{
	opacity: 0.8;
}
.slider-btn-group .slide-btn-refresh{
	color:rgba(14,107,176,1);
	border-left: 1px solid #ddd
}	
</style>
